//
//  MobileRegisterViewController.swift
//  Exchange
//
//  Created by rui on 2018/4/24.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class MobileRegisterViewController: AccountBaseViewController,InputViewDelegate {

    @IBOutlet weak var titleL: UILabel!
    @IBOutlet weak var phoneInput: InputView!
    @IBOutlet weak var imageCodeInput: InputView!
    @IBOutlet weak var readedBtn: UIButton!
    @IBOutlet weak var serverDetailBtn: UIButton!
    @IBOutlet weak var nextBtn: SureButton!
    
    @IBOutlet weak var emailRegisterBtn: UIButton!
    @IBOutlet weak var loginBtn: UIButton!
    
    lazy var registerViewModel:RegisterOrResetViewModel = RegisterOrResetViewModel()

    override func viewDidLoad() {
        super.viewDidLoad()
        initUI()
    }
    func initUI() {
        self.titleL.text = kText("手机注册")
        self.phoneInput.delegate = self
        self.phoneInput.placeholder = kText("请输入手机号")
        self.phoneInput.input.keyboardType = .numberPad
        self.phoneInput.style = .title
        self.phoneInput.title = "+86"
        self.phoneInput.image = #imageLiteral(resourceName: "china")
        self.imageCodeInput.delegate = self
        self.imageCodeInput.style = .imageVerify
        self.imageCodeInput.placeholder = kText("请输入图片验证码")
        
//        let text = kText("已阅读并同意《C2C产品服务条款》")
//        let attributedString = NSMutableAttributedString(string:text)
//
//        let range = (text as NSString).range(of: "《")
//        if range.location != NSNotFound {
//            if ThemeManager.default.currentThemeMode() == .day {
//                attributedString.addAttributes([
//                    NSAttributedStringKey.foregroundColor:kTitleColor
//                    ], range: NSRange(location: 0, length: range.location))
//            }else {
//                attributedString.addAttributes([
//                    NSAttributedStringKey.foregroundColor:UIColor(hexStr: "#e7ebf4")
//                    ], range: NSRange(location: 0, length: range.location))
//            }
//            attributedString.addAttributes([
//                NSAttributedStringKey.foregroundColor:kThemeColor
//                ], range: NSRange(location: range.location, length: attributedString.length - range.location))
//        }
//
//        self.serverDetailBtn.setAttributedTitle(attributedString, for: .normal)
        readedBtn.isSelected = true
        readedBtn.isHidden = true
        self.serverDetailBtn.isHidden = true
        self.nextBtn.setTitle(kText("下一步"), for: .normal)
        self.emailRegisterBtn.setTitle(kText("邮箱注册"), for: .normal)
        self.loginBtn.setTitle(kText("登录"), for: .normal)
        self.nextBtn.isEnabled = false
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        initData()
    }
    func initData() {
        registerViewModel.response = {[weak self](state,isSuccess,errorMsg)in
            if state == ResponseState.mine_imageVerify {
                if isSuccess {
                   self?.imageCodeInput.imageCode = self?.registerViewModel.image
                }else {
                    if let msg = errorMsg {
                        CYMBShow.showMessage(msg)
                    }
                }
            }else if state == ResponseState.mine_mobileCode {
                if isSuccess {
                    let vc = SecondRegisterOrResetViewController()
                    vc.type = 0
                    vc.viewModel = self?.registerViewModel
                    self?.navigationController?.pushViewController(vc, animated: true)
                }else {
                    if let msg = errorMsg {
                        CYMBShow.showMessage(msg)
                    }
                }
            }
        }
        registerViewModel.getImageCode()
    }
    // MARK: - event
    @IBAction func readed(_ sender: UIButton) {
        sender.isSelected = !sender.isSelected
        changeSureBtn()
    }
    
    @IBAction func next(_ sender: SureButton) {
        dPrint("next")
        let mobile = self.phoneInput.input.text ?? ""
        if mobile.isPhone() == false {
            CYMBShow.showMessage(kText("手机号格式不正确"))
            return
        }
        self.view.endEditing(true)
        var areaCode = self.phoneInput.title ?? "+86"
        areaCode.removeFirst()
        self.registerViewModel.sendMobileCode(mobile: mobile, areaCode: areaCode, imageCode: self.imageCodeInput.input.text!)
    }
    
    @IBAction func server(_ sender: UIButton) {
        dPrint("server")
    }
    
    @IBAction func emailRegister(_ sender: UIButton) {
        self.navigationController?.customReplaceTopPushViewController(EmailRegisterViewController(), animated: true)
        
    }
    
    @IBAction func login(_ sender: UIButton) {
        self.navigationController?.popViewController(animated: true)
    }
    
    func inputViewTextDidChange(inputView: InputView) {
        dPrint("inputViewTextDidChange")
        changeSureBtn()
    }
    
    func inputViewImageVerifyClick(inputView: InputView) {
        dPrint("inputViewImageVerifyClick")
        self.registerViewModel.getImageCode()
    }
    
   
    func inputViewTitleClick(inputView: InputView) {
        dPrint("inputViewTitleClick")
        let vc = CountrySelectedViewController()
        vc.delegate = self
        vc.selectedView.isAutoPop = true
        self.view.endEditing(true)
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    func changeSureBtn() {
        if let phoneCount = self.phoneInput.input.text?.count,let imageCodeCount = self.imageCodeInput.input.text?.count, phoneCount > 0,  imageCodeCount > 0,self.readedBtn.isSelected == true {
            self.nextBtn.isEnabled = true
        }else {
            self.nextBtn.isEnabled = false
        }
    }
}
extension MobileRegisterViewController: CountrySelectDelegate {
    func selectCountry(model: CountryModel) {
        self.phoneInput.image = UIImage(named: model.countryFlagName)
        self.phoneInput.title = model.countryCode
    }
}
